﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace SVAP
{
    public partial class RecentPictureForm : Form
    {
        private static string strExePath = "";// ".exe"文件所在路径
        private int count;  //计数器

        public RecentPictureForm()
        {
            InitializeComponent();

            //将该窗口设置为程序的焦点，且不允许改变，直至关闭该窗口
            //SetWindow setWin = new SetWindow(this.Handle);
            //setWin.Star();

            //底层控件不可见
            pictureBox1.Visible = false;
            pictureBox2.Visible = false;
            pictureBox3.Visible = false;
            pictureBox4.Visible = false;
            pictureBox5.Visible = false;
            pictureBox6.Visible = false;
        }

        #region 显示效果控制

        //当鼠标移动过TopLeft图片控件时，显示该控件的边缘
        private void pictureBox_TopLeft_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox1.Visible = true;
        }

        //当鼠标离开TopLeft图片控件时，不显示该控件的边缘
        private void pictureBox_TopLeft_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Visible = false;
        }

        //当鼠标移过TopMiddle图片控件时，显示该控件边缘
        private void pictureBox_TopMiddle_MouseEnter(object sender, EventArgs e)
        {
            pictureBox2.Visible = true;
        }

        //当鼠标移过TopMiddle图片控件时，不显示该控件的边缘
        private void pictureBox_TopMiddle_MouseLeave(object sender, EventArgs e)
        {
            pictureBox2.Visible = false;
        }

        //当鼠标移过TopRight图片控件时，显示该控件边缘
        private void pictureBox_TopRight_MouseEnter(object sender, EventArgs e)
        {
            pictureBox3.Visible = true;
        }

        //当鼠标移过TopRight图片控件时，不显示该控件的边缘
        private void pictureBox_TopRight_MouseLeave(object sender, EventArgs e)
        {
            pictureBox3.Visible = false;
        }

        //当鼠标移过BaseLeft图片控件时，显示该控件边缘
        private void pictureBox_BaseLeft_MouseEnter(object sender, EventArgs e)
        {
            pictureBox4.Visible = true;
        }

        //当鼠标移过BaseLeft图片控件时，不显示该控件的边缘
        private void pictureBox_BaseLeft_MouseLeave(object sender, EventArgs e)
        {
            pictureBox4.Visible = false;
        }

        //当鼠标移过BaseMiddle图片控件时，显示该控件边缘
        private void pictureBox_BaseMiddle_MouseEnter(object sender, EventArgs e)
        {
            pictureBox5.Visible = true;
        }

        //当鼠标移过BaseMiddle图片控件时，不显示该控件的边缘
        private void pictureBox_BaseMiddle_MouseLeave(object sender, EventArgs e)
        {
            pictureBox5.Visible = false;
        }

        //当鼠标移过BaseRight图片控件时，显示该控件边缘
        private void pictureBox_BaseRight_MouseEnter(object sender, EventArgs e)
        {
            pictureBox6.Visible = true;
        }

        //当鼠标移过BaseRight图片控件时，不显示该控件的边缘
        private void pictureBox_BaseRight_MouseLeave(object sender, EventArgs e)
        {
            pictureBox6.Visible = false;
        }

        //当鼠标移动到Label_TopLeft上时，字体显示为红色
        private void label_TopLeft_MouseEnter(object sender, EventArgs e)
        {
            //label_TopLeft.Font.Underline = true;
            label_TopLeft.ForeColor = Color.Red; 
        }

        //当鼠标移出Label_TopLeft上时，字体恢复为黑色
        private void label_TopLeft_MouseLeave(object sender, EventArgs e)
        {
            label_TopLeft.ForeColor = Color.Black;
        }

        //当鼠标移动到Label_TopMiddle上时，字体显示为红色
        private void label_TopMiddle_MouseEnter(object sender, EventArgs e)
        {
            label_TopMiddle.ForeColor = Color.Red;
        }

        //当鼠标移出Label_TopMiddle上时，字体恢复为黑色
        private void label_TopMiddle_MouseLeave(object sender, EventArgs e)
        {
            label_TopMiddle.ForeColor = Color.Black;
        }

        //当鼠标移动到Label_TopRight上时，字体显示为红色
        private void label_TopRight_MouseEnter(object sender, EventArgs e)
        {
            label_TopRight.ForeColor = Color.Red;
        }

        //当鼠标移出Label_TopRight上时，字体恢复为黑色
        private void label_TopRight_MouseLeave(object sender, EventArgs e)
        {
            label_TopRight.ForeColor = Color.Black;
        }

        //当鼠标移动到Label_BaseLeft上时，字体显示为红色
        private void label_BaseLeft_MouseEnter(object sender, EventArgs e)
        {
            label_BaseLeft.ForeColor = Color.Red;
        }

        //当鼠标移出Label_BaseLeft上时，字体恢复为黑色
        private void label_BaseLeft_MouseLeave(object sender, EventArgs e)
        {
            label_BaseLeft.ForeColor = Color.Black;
        }

        //当鼠标移动到Label_BaseMiddle上时，字体显示为红色
        private void label_BaseMiddle_MouseEnter(object sender, EventArgs e)
        {
            label_BaseMiddle.ForeColor = Color.Red;
        }

        //当鼠标移出Label_BaseMiddle上时，字体恢复为黑色
        private void label_BaseMiddle_MouseLeave(object sender, EventArgs e)
        {
            label_BaseMiddle.ForeColor = Color.Black;
        }

        //当鼠标移动到Label_BaseRight上时，字体显示为红色
        private void label_BaseRight_MouseEnter(object sender, EventArgs e)
        {
            label_BaseRight.ForeColor = Color.Red;
        }

        //当鼠标移出Label_BaseRight上时，字体恢复为黑色
        private void label_BaseRight_MouseLeave(object sender, EventArgs e)
        {
            label_BaseRight.ForeColor = Color.Black;
        }

#endregion


        //单击TopLeft图片控件，打开图片分析窗口
        private void pictureBox_TopLeft_Click(object sender, EventArgs e)
        {
            MainForm mf = new MainForm();
            mf = (MainForm)this.Owner;

            try
            {
                //修改频率
                ModifyFre mff = new ModifyFre();
                mff.ModifyFrePic((string)pictureBox_TopLeft.Image.Tag);

                pictureMDI pmdi = new pictureMDI();
                pmdi.MdiParent = mf;
                pmdi.filePath = (string)pictureBox_TopLeft.Image.Tag;  //获取选定的图片文件的路径

                //在缩微窗口显示图片
                Bitmap selectedPicture = new Bitmap(pmdi.filePath);

                pmdi.pictureBox_picMDI.Image = (Image)selectedPicture;

                //显示图片的属性信息
                FileInfo fi = new FileInfo(pmdi.filePath);  //文件的大小
                pmdi.lbl_picMDI_att.Text = "名称: " + Path.GetFileName(pmdi.filePath) + "\r\n" + "\r\n" + "大小: " + fi.Length.ToString() + "字节\r\n" + "\r\n" + "尺寸: " + selectedPicture.Size.Height.ToString() + " x " + selectedPicture.Size.Width.ToString() + "\r\n" + "\r\n" + "修改日期: " + fi.LastWriteTime.ToString();

                pmdi.WindowState = FormWindowState.Maximized;
                pmdi.Show();

                this.Hide();   //隐藏最近使用图片窗口
            }
            catch
            {
                MessageBox.Show("打开指定文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        //单击TopLeft标签，打开图片分析窗口
        private void label_TopLeft_Click(object sender, EventArgs e)
        {
            //显示图片
            pictureBox_TopLeft_Click(sender, e);
        }

        //加载窗体,显示最近使用过的图片
        private void RecentPictureForm_Load(object sender, EventArgs e)
        {
            try
            {
                strExePath = System.AppDomain.CurrentDomain.BaseDirectory;    //获取程序当前文件夹地址
                strExePath += @"RecentPictures.txt";  //找到保存最近图片信息的文件

                ////测试用
                //strExePath += @"2round.bmp";  //找到保存最近图片信息的文件
                //Bitmap selectedPicture = new Bitmap(strExePath);
                //selectedPicture.Tag = strExePath;
                //pictureBox_TopLeft.Image = selectedPicture;

                //从最近使用图片文件中读取图片的使用信息
                StreamReader sr = new StreamReader(strExePath, System.Text.Encoding.Default);

                char[] splitChar = { '*' };

                Dictionary<string, int> RecentPicture = new Dictionary<string, int>();

                string sLine = "";
                sLine = sr.ReadLine();
                if (sLine != null)
                {
                    //string[] strSplit = sLine.Split(new char[] { '&' });
                    string[] strSplit = sLine.Split(splitChar);
                    //添加进词典
                    RecentPicture.Add(strSplit[0].ToString(), int.Parse(strSplit[1].ToString()));

                    while (sLine != null)
                    {
                        sLine = sr.ReadLine();
                        if (sLine != null)
                        {
                            //strSplit = sLine.Split(new char[] { '&' });
                            strSplit = sLine.Split(splitChar);
                            //添加进词典
                            RecentPicture.Add(strSplit[0].ToString(), int.Parse(strSplit[1].ToString()));
                        }
                    }
                }

                sr.Close();

                string[] picture = new string[RecentPicture.Count];
                int[] fre = new int[RecentPicture.Count];

                //复制并排序
                RecentPicture.Values.CopyTo(fre, 0);
                RecentPicture.Keys.CopyTo(picture, 0);
                System.Array.Sort(fre, picture);
                //System.Array.Reverse(fre);
                //System.Array.Reverse(picture);

                count = 0;  //计数器清0

                //显示
                #region
                for (int i = RecentPicture.Count - 1; i >= 0; i--)
                {
                    if (count > 5)
                        break;

                    if (fre[i] != 0 && count < 5)
                    {
                        if (count == 0)
                        {
                            //在top_left控件中显示图片
                            Bitmap selectedPicture = new Bitmap(picture[i]);  //在缩微窗口显示图片
                            selectedPicture.Tag = picture[i];
                            pictureBox_TopLeft.Image = (Image)selectedPicture;
                            label_TopLeft.Text = Path.GetFileName(selectedPicture.Tag.ToString());
                            count++;
                        }
                        else
                        {
                            if (count == 1)
                            {
                                //在top_middle控件中显示图片
                                Bitmap selectedPicture = new Bitmap(picture[i]);  //在缩微窗口显示图片
                                selectedPicture.Tag = picture[i];
                                pictureBox_TopMiddle.Image = (Image)selectedPicture;
                                label_TopMiddle.Text = Path.GetFileName(selectedPicture.Tag.ToString());
                                count++;
                            }
                            else
                            {
                                if (count == 2)
                                {
                                    //在top_right控件中显示图片
                                    Bitmap selectedPicture = new Bitmap(picture[i]);  //在缩微窗口显示图片
                                    selectedPicture.Tag = picture[i];
                                    pictureBox_TopRight.Image = (Image)selectedPicture;
                                    label_TopRight.Text= Path.GetFileName(selectedPicture.Tag.ToString());
                                    count++;
                                }
                                else
                                {
                                    if (count == 3)
                                    {
                                        //在base_left控件中显示图片
                                        Bitmap selectedPicture = new Bitmap(picture[i]);  //在缩微窗口显示图片
                                        selectedPicture.Tag = picture[i];
                                        pictureBox_BaseLeft.Image = (Image)selectedPicture;
                                        label_BaseLeft.Text = Path.GetFileName(selectedPicture.Tag.ToString());
                                        count++;
                                    }
                                    else
                                    {
                                        if (count == 4)
                                        {
                                            //在base_middle控件中显示图片
                                            Bitmap selectedPicture = new Bitmap(picture[i]);  //在缩微窗口显示图片
                                            selectedPicture.Tag = picture[i];
                                            pictureBox_BaseMiddle.Image = (Image)selectedPicture;
                                            label_BaseMiddle.Text = Path.GetFileName(selectedPicture.Tag.ToString());
                                            count++;
                                        }
                                        else
                                        {
                                            if (count == 5)
                                            {
                                                //在base_right控件中显示图片
                                                Bitmap selectedPicture = new Bitmap(picture[i]);  //在缩微窗口显示图片
                                                selectedPicture.Tag = picture[i];
                                                pictureBox_BaseRight.Image = (Image)selectedPicture;
                                                label_BaseRight.Text = Path.GetFileName(selectedPicture.Tag.ToString());
                                                count++;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }
                }//end for
                #endregion

            }
            catch
            {
                MessageBox.Show("无法打开图片!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //单击“更多图片”
        private void label_MorePicture_Click(object sender, EventArgs e)
        {
            MainForm mf = new MainForm();
            mf = (MainForm)this.Owner;

            //打开文件对话框
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.FileName = "";
            ofd.Filter = "bmp图片(*.bmp)|*.bmp|所有文件(*.*)|*.*";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //this.Hide();   //隐藏最近使用图片窗口

                try
                {
                    //修改频率
                    ModifyFre mff = new ModifyFre();
                    mff.ModifyFrePic(ofd.FileName);

                    pictureMDI pmdi = new pictureMDI();
                    pmdi.MdiParent = mf;
                    pmdi.filePath = ofd.FileName;  //获取选定的图片文件的路径

                    //在缩微窗口显示图片
                    Bitmap selectedPicture = new Bitmap(ofd.FileName);

                    pmdi.pictureBox_picMDI.Image = (Image)selectedPicture;

                    //显示图片的属性信息
                    FileInfo fi = new FileInfo(ofd.FileName);  //文件的大小
                    pmdi.lbl_picMDI_att.Text = "名称: " + Path.GetFileName(ofd.FileName) + "\r\n" + "\r\n" + "大小: " + fi.Length.ToString() + "字节\r\n" + "\r\n" + "尺寸: " + selectedPicture.Size.Height.ToString() + " x " + selectedPicture.Size.Width.ToString() + "\r\n" + "\r\n" + "修改日期: " + fi.LastWriteTime.ToString();

                    pmdi.WindowState = FormWindowState.Maximized;
                    pmdi.Show();

                    this.Hide();   //隐藏最近使用图片窗口
                }
                catch
                {
                    MessageBox.Show("打开指定文件失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        //单击TopMiddle图片控件，打开图片分析窗口
        private void pictureBox_TopMiddle_Click(object sender, EventArgs e)
        {
            MainForm mf = new MainForm();
            mf = (MainForm)this.Owner;

            try
            {
                //修改频率
                ModifyFre mff = new ModifyFre();
                mff.ModifyFrePic((string)pictureBox_TopMiddle.Image.Tag);

                pictureMDI pmdi = new pictureMDI();
                pmdi.MdiParent = mf;
                pmdi.filePath = (string)pictureBox_TopMiddle.Image.Tag;  //获取选定的图片文件的路径

                //在缩微窗口显示图片
                Bitmap selectedPicture = new Bitmap(pmdi.filePath);

                pmdi.pictureBox_picMDI.Image = (Image)selectedPicture;

                //显示图片的属性信息
                FileInfo fi = new FileInfo(pmdi.filePath);  //文件的大小
                pmdi.lbl_picMDI_att.Text = "名称: " + Path.GetFileName(pmdi.filePath) + "\r\n" + "\r\n" + "大小: " + fi.Length.ToString() + "字节\r\n" + "\r\n" + "尺寸: " + selectedPicture.Size.Height.ToString() + " x " + selectedPicture.Size.Width.ToString() + "\r\n" + "\r\n" + "修改日期: " + fi.LastWriteTime.ToString();

                pmdi.WindowState = FormWindowState.Maximized;
                pmdi.Show();

                this.Hide();   //隐藏最近使用图片窗口
            }
            catch
            {
                MessageBox.Show("打开指定文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        //单击TopMiddle标签，打开图片分析窗口
        private void label_TopMiddle_Click(object sender, EventArgs e)
        {
            pictureBox_TopMiddle_Click(sender, e);
        }

        //单击TopRight图片控件，打开图片分析窗口
        private void pictureBox_TopRight_Click(object sender, EventArgs e)
        {
            MainForm mf = new MainForm();
            mf = (MainForm)this.Owner;

            try
            {
                //修改频率
                ModifyFre mff = new ModifyFre();
                mff.ModifyFrePic((string)pictureBox_TopRight.Image.Tag);

                pictureMDI pmdi = new pictureMDI();
                pmdi.MdiParent = mf;
                pmdi.filePath = (string)pictureBox_TopRight.Image.Tag;  //获取选定的图片文件的路径

                //在缩微窗口显示图片
                Bitmap selectedPicture = new Bitmap(pmdi.filePath);

                pmdi.pictureBox_picMDI.Image = (Image)selectedPicture;

                //显示图片的属性信息
                FileInfo fi = new FileInfo(pmdi.filePath);  //文件的大小
                pmdi.lbl_picMDI_att.Text = "名称: " + Path.GetFileName(pmdi.filePath) + "\r\n" + "\r\n" + "大小: " + fi.Length.ToString() + "字节\r\n" + "\r\n" + "尺寸: " + selectedPicture.Size.Height.ToString() + " x " + selectedPicture.Size.Width.ToString() + "\r\n" + "\r\n" + "修改日期: " + fi.LastWriteTime.ToString();

                pmdi.WindowState = FormWindowState.Maximized;
                pmdi.Show();

                this.Hide();   //隐藏最近使用图片窗口
            }
            catch
            {
                MessageBox.Show("打开指定文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        //单击TopRight标签，打开图片分析窗口
        private void label_TopRight_Click(object sender, EventArgs e)
        {
            pictureBox_TopMiddle_Click(sender, e);
        }

        //单击BaseLeft图片控件，打开图片分析窗口
        private void pictureBox_BaseLeft_Click(object sender, EventArgs e)
        {
            MainForm mf = new MainForm();
            mf = (MainForm)this.Owner;

            try
            {
                //修改频率
                ModifyFre mff = new ModifyFre();
                mff.ModifyFrePic((string)pictureBox_BaseLeft.Image.Tag);

                pictureMDI pmdi = new pictureMDI();
                pmdi.MdiParent = mf;
                pmdi.filePath = (string)pictureBox_BaseLeft.Image.Tag;  //获取选定的图片文件的路径

                //在缩微窗口显示图片
                Bitmap selectedPicture = new Bitmap(pmdi.filePath);

                pmdi.pictureBox_picMDI.Image = (Image)selectedPicture;

                //显示图片的属性信息
                FileInfo fi = new FileInfo(pmdi.filePath);  //文件的大小
                pmdi.lbl_picMDI_att.Text = "名称: " + Path.GetFileName(pmdi.filePath) + "\r\n" + "\r\n" + "大小: " + fi.Length.ToString() + "字节\r\n" + "\r\n" + "尺寸: " + selectedPicture.Size.Height.ToString() + " x " + selectedPicture.Size.Width.ToString() + "\r\n" + "\r\n" + "修改日期: " + fi.LastWriteTime.ToString();

                pmdi.WindowState = FormWindowState.Maximized;
                pmdi.Show();

                this.Hide();   //隐藏最近使用图片窗口
            }
            catch
            {
                MessageBox.Show("打开指定文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        //单击BaseLeft标签，打开图片分析窗口
        private void label_BaseLeft_Click(object sender, EventArgs e)
        {
            pictureBox_BaseLeft_Click(sender, e);
        }

        //单击BaseMiddle图片控件，打开图片分析窗口
        private void pictureBox_BaseMiddle_Click(object sender, EventArgs e)
        {
            MainForm mf = new MainForm();
            mf = (MainForm)this.Owner;

            try
            {
                //修改频率
                ModifyFre mff = new ModifyFre();
                mff.ModifyFrePic((string)pictureBox_BaseMiddle.Image.Tag);

                pictureMDI pmdi = new pictureMDI();
                pmdi.MdiParent = mf;
                pmdi.filePath = (string)pictureBox_BaseMiddle.Image.Tag;  //获取选定的图片文件的路径

                //在缩微窗口显示图片
                Bitmap selectedPicture = new Bitmap(pmdi.filePath);

                pmdi.pictureBox_picMDI.Image = (Image)selectedPicture;

                //显示图片的属性信息
                FileInfo fi = new FileInfo(pmdi.filePath);  //文件的大小
                pmdi.lbl_picMDI_att.Text = "名称: " + Path.GetFileName(pmdi.filePath) + "\r\n" + "\r\n" + "大小: " + fi.Length.ToString() + "字节\r\n" + "\r\n" + "尺寸: " + selectedPicture.Size.Height.ToString() + " x " + selectedPicture.Size.Width.ToString() + "\r\n" + "\r\n" + "修改日期: " + fi.LastWriteTime.ToString();

                pmdi.WindowState = FormWindowState.Maximized;
                pmdi.Show();

                this.Hide();   //隐藏最近使用图片窗口
            }
            catch
            {
                MessageBox.Show("打开指定文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        //单击BaseMiddle标签，打开图片分析窗口
        private void label_BaseMiddle_Click(object sender, EventArgs e)
        {
            pictureBox_BaseMiddle_Click(sender, e);
        }

        //单击BaseRight图片控件，打开图片分析窗口
        private void pictureBox_BaseRight_Click(object sender, EventArgs e)
        {
            MainForm mf = new MainForm();
            mf = (MainForm)this.Owner;

            try
            {
                //修改频率
                ModifyFre mff = new ModifyFre();
                mff.ModifyFrePic((string)pictureBox_BaseRight.Image.Tag);

                pictureMDI pmdi = new pictureMDI();
                pmdi.MdiParent = mf;
                pmdi.filePath = (string)pictureBox_BaseRight.Image.Tag;  //获取选定的图片文件的路径

                //在缩微窗口显示图片
                Bitmap selectedPicture = new Bitmap(pmdi.filePath);

                pmdi.pictureBox_picMDI.Image = (Image)selectedPicture;

                //显示图片的属性信息
                FileInfo fi = new FileInfo(pmdi.filePath);  //文件的大小
                pmdi.lbl_picMDI_att.Text = "名称: " + Path.GetFileName(pmdi.filePath) + "\r\n" + "\r\n" + "大小: " + fi.Length.ToString() + "字节\r\n" + "\r\n" + "尺寸: " + selectedPicture.Size.Height.ToString() + " x " + selectedPicture.Size.Width.ToString() + "\r\n" + "\r\n" + "修改日期: " + fi.LastWriteTime.ToString();

                pmdi.WindowState = FormWindowState.Maximized;
                pmdi.Show();

                this.Hide();   //隐藏最近使用图片窗口
            }
            catch
            {
                MessageBox.Show("打开指定文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        //单击BaseRight标签，打开图片分析窗口
        private void label_BaseRight_Click(object sender, EventArgs e)
        {
            pictureBox_BaseRight_Click(sender, e);
        }


        

    }
}
